package com.zgroup.domain

class User {
  final static String COOKIE_USER_NAME = "COOKIE_USER_NAME" // 用户名 Cookie Key
  final static String COOKIE_PASSWORD = "COOKIE_PASSWORD" // 用户密码 Cookie Key

  final static String REQUEST_USER_INFO = "userInfo"

  final static String UPLOAD_FILE_PATH = "/upload/head_picture/"     //发布信息图片上传路径
  final static String UPLOAD_THUMBNAIL_PATH = "/upload/head_thubnail/"     //发布信息缩略图上传路径
  final static String DEFUALT_USER_HEAD = "/images/ui/default_user_head.gif"
  final static int INFO_CACHE_SIZE = 8

  final static String STATE_GENERAL = "10"    //普通状态
  final static String STATE_AGENT = "20"    //经纪人状态
  final static String STATE_AGENT_APPLY = "21"    //经纪人申请状态
  final static String STATE_AGENT_APPLY_FAIL = "22"   //经纪人申请失败状态

  List randomInfoList

  def userService

  String username     // 用户名 *注：用户注册时，用户名和邮箱一致
  String password     // 密码
  //String nickname     // 昵称

  // 个人信息
  String name         // 姓名
  String job          // 职业
  String mobile       // 手机
  String telephone    // 固定电话
  String msn          // MSN
  String qq           // QQ
  String gtalk        // GTALK
  String email        // 邮箱
  String title        // 概括标题
  String homepage     // 个人主页
  String headPicture  // 个人头像

  String homeEstatePrivacyFlag = "1"      // 家的物业保密标记 0：公开 1：保密
  String companyEstatePrivacyFlag = "1"   // 公司的物业保密标记 0：公开 1：保密
  String companyPrivacyFlag = "1"         // 公司保密标记 0：公开 1：保密
  Estate homeEstate
  Estate companyEstate
  //BusinessInfo businessInfo
  Company company

  BigDecimal availableMoney = new BigDecimal("0")   // 可用资金
  BigDecimal freezingMoney = new BigDecimal("0")    // 冻结资金
  BigDecimal spentMoney = new BigDecimal("0")       // 已消费资金

  BigDecimal availablePoint = new BigDecimal("0")   // 可用积分
  BigDecimal freezingPoint = new BigDecimal("0")    // 冻结积分
  BigDecimal spentPoint = new BigDecimal("0")       // 已消费积分

  BigDecimal availableCoupon = new BigDecimal("0")   // 可用点券
  BigDecimal freezingCoupon = new BigDecimal("0")    // 冻结点券
  BigDecimal spentCoupon = new BigDecimal("0")       // 已消费点券

  Integer acceptPicCount = 0//入库图片数
  String state = STATE_GENERAL

  Date dateCreated
  Date lastUpdated

  String getDisplayName() {
    if (name == null) {
      int pos = username.lastIndexOf("@")
      if (pos > -1) {
        return username.substring(0, pos)
      }
      return username
    }
    return name
  }

  String getHeadImage() {
    if (headPicture == null) {
      DEFUALT_USER_HEAD
    } else {
      UPLOAD_FILE_PATH + headPicture
    }
  }

  Integer getInfoCount() {
    return infoReleases?.size()
  }

  Integer getPublishedInfoCount() {
    return userService.countPublishedInfo(this)
//        Integer count=0
//        def results = InfoRelease.findAllByPublisher(this)
//        results.each{
//            count+=it.estates.size()
//        }
//        return count
  }

  InfoRelease getRandomInfo() {
    if (randomInfoList == null) {
      randomInfoList = userService.randomInfo(this, INFO_CACHE_SIZE)
      if (randomInfoList == null) {
        randomInfoList = new ArrayList()
        return null
      }
    }

    if (randomInfoList.isEmpty()) {
      return null
    }
    int randomIndex = new Random().nextInt(randomInfoList.size())

    return randomInfoList.get(randomIndex)
    //return results?.get(0)
  }

  Integer getBidCount() {
    return Bid.executeQuery("select count(*) as c from Bid as b where b.user=? and b.status=? and b.expired=?", [this, "10", "0"]).getAt(0)
  }

  Integer getEstateCount() {
    return Bid.executeQuery("select count(*) as c from Bid as b where b.user=?", [this]).getAt(0);
  }

  boolean getIsComplete() {
    return homeEstate != null && companyEstate != null
  }

  static transients = ['headImage', 'displayName', 'infoCount', 'publishedInfoCount', 'bidCount', 'randomInfo', 'isComplete', 'estateCount']

  static hasMany = [infoReleases: InfoRelease, bids: Bid]
  //static belongsTo =[homeEstate:Estate,companyEstate:Estate,company:Company]

  static constraints = {
    username(blank: false, email: true, unique: true, maxSize: 50)
    password(blank: false, maxSize: 32)
    //nickname(blank:false, maxSize:30)
    name(nullable: true, maxSize: 10)
    job(nullable: true, maxSize: 50)
    mobile(nullable: true, matches: "[0-9]+", maxSize: 30)
    telephone(nullable: true, maxSize: 30)
    msn(nullable: true, email: true, maxSize: 50)
    qq(nullable: true, matches: "[0-9]+", maxSize: 20)
    gtalk(nullable: true, maxSize: 20)
    email(blank: false, email: true, maxSize: 50)
    title(nullable: true, maxSize: 50)
    homepage(nullable: true, maxSize: 500)
    headPicture(nullable: true, maxSize: 255)

    availableMoney(blank: false, range: 0..99999999, scale: 2)
    freezingMoney(blank: false, range: 0..99999999, scale: 2)
    spentMoney(blank: false, range: 0..99999999, scale: 2)
    availablePoint(blank: false, range: 0..9999999999, scale: 0)
    freezingPoint(blank: false, range: 0..9999999999, scale: 0)
    spentPoint(blank: false, range: 0..9999999999, scale: 0)
    availableCoupon(blank: false, range: 0..9999999999, scale: 0)
    freezingCoupon(blank: false, range: 0..9999999999, scale: 0)
    spentCoupon(blank: false, range: 0..9999999999, scale: 0)

    homeEstatePrivacyFlag(blank: false, maxSize: 6)
    companyEstatePrivacyFlag(blank: false, maxSize: 6)
    companyPrivacyFlag(blank: false, maxSize: 6)
    // businessInfo(nullable:true)
    company(nullable: true)
    homeEstate(nullable: true)
    companyEstate(nullable: true)
    acceptPicCount(nullable: true)
    state(blank: false, maxSize: 6)
  }

  static mapping = {
    dynamicInsert true
    dynamicUpdate true
  }

  String toString() {
    "id:" + id + ",username:" + username + ",name:" + name
  }

  boolean isPayable(BigDecimal price, String payment) {
    if ("10" == payment) {
      return availablePoint >= price
    } else if ("20" == payment) {
      return availableCoupon >= price
    } else {
      return availableMoney >= price
    }
  }

  void addPhotoPresented() {
    def setting = Setting.get(1)
    availableMoney += setting.photoMoneyPresented
    availablePoint += setting.photoPointPresented
    availableCoupon += setting.photoCouponPresented
    acceptPicCount = acceptPicCount == null ? 1 : acceptPicCount++

    save()
  }
}